//
//  ElectricityManager.m
//  EasyToo
//
//  Created by YuanDee on 13-9-14.
//
//

#import "CheckElectricChargeInfoRequest.h"
#import "CheckElectricChargeInfoResponse.h"
#import "ServerCommunicationManager.h"
#import "ElectricityManager.h"
#import "ElectricChargeRequest.h"
#import "ElectricChargeResponse.h"

@implementation ElectricityManager

static ElectricityManager *eManager = nil;


+ (ElectricityManager*)shareManager
{
    if (eManager == nil) {
        eManager = [[ElectricityManager alloc] init];
    }
    return eManager;
}

- (void)checkElectricInfo:(NSString *)clientId
{
    CheckElectricChargeInfoRequest *request =[[CheckElectricChargeInfoRequest alloc] initWithUserAccount:clientId Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

-(void)orderElectricWithClientId:(NSString*)clientId
                   andClientName:(NSString*)clientName
                     andBillDate:(NSString*)billDate
                   andBillMonths:(NSString*)billMonths
                     andTotalFee:(NSString*)totalFee
{
    ElectricChargeRequest* request = [[ElectricChargeRequest alloc]initWithClientId:clientId andClientName:clientName andBillDate:billDate andBillMonths:billMonths andTotalFee:totalFee Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];

}
- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response isKindOfClass:[CheckElectricChargeInfoResponse class]])
    {
        [self handleElectricityCheckSuccess:(CheckElectricChargeInfoResponse*)response];
    }
    else if ([response isKindOfClass:[ElectricChargeResponse class]])
    {
        [self handleElectricityOrderSuccess:(ElectricChargeResponse*)response];
    }
    
}

- (void)onRequestFail:(BaseResponse *)response
{
    if ([response isKindOfClass:[CheckElectricChargeInfoResponse class]])
    {
        [self.delegate onCheckElectricInfoFinish:NO electricFeeInfoArray:nil];
    }
    else if ([response isKindOfClass:[ElectricChargeResponse class]])
    {
        [self.delegate onOrderElectricFinish:NO Tn:nil];
    }
    
}

- (void)handleElectricityCheckSuccess:(CheckElectricChargeInfoResponse*)response
{
    if (response.electricFeeDetailInfoArray)
    {
        [self.delegate onCheckElectricInfoFinish:YES electricFeeInfoArray:response.electricFeeDetailInfoArray];
    }
    else
    {
        [self.delegate onCheckElectricInfoFinish:NO electricFeeInfoArray:nil];
    }
}
- (void)handleElectricityOrderSuccess:(ElectricChargeResponse*)response
{
    NSString *tn = response.tn;
    
    if (tn)
    {
        [self.delegate onOrderElectricFinish:YES Tn:tn];
    }
    else
    {
        [self.delegate onOrderElectricFinish:NO Tn:nil];
    }

}
@end
